package cn.swpu.blog.exception;

import cn.swpu.blog.enums.ResponseEnum;
import cn.swpu.blog.vo.ResponseVo;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author 冰翔
 * @Date Created in 3:34 下午 2020/12/11
 */
@ControllerAdvice
public class RuntimeExceptionHandler {

    @ExceptionHandler(CategoryNotFoundException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseVo categoryNotFound(CategoryNotFoundException e){
        return ResponseVo.error(ResponseEnum.CATEGORY_NOT_FOUND, e.getMsg());
    }

    @ExceptionHandler(TagNotFoundException.class)
    @ResponseBody
    public ResponseVo tagNotFound(TagNotFoundException e){
        return ResponseVo.error(ResponseEnum.TAG_NOT_FOUND, e.getMsg());
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public ResponseVo sqlTagNotFound(SQLIntegrityConstraintViolationException e) {
        return ResponseVo.error(ResponseEnum.CATEGORY_TAG_NOT_FOUND);
    }

    @ExceptionHandler(BlogNotFoundException.class)
    @ResponseBody
    public ResponseVo blogNotFoundException(BlogNotFoundException e) {
        return ResponseVo.error(ResponseEnum.BLOG_NOT_FOUND, e.getMsg());
    }

    @ExceptionHandler(StateErrorException.class)
    @ResponseBody
    public ResponseVo crsfException(StateErrorException e) {
        return ResponseVo.error(ResponseEnum.CSRF_STATE_EXCEPTION, e.getMsg());
    }
}
